package goleetcode

// 枚举两条直角边的交点，然后将该点所在行的其他点与该点所在列的其他点一一匹配
func numberOfRightTriangles(grid [][]int) int64 {
	n := len(grid)
	m := len(grid[0])
	col := make([]int, m)
	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			col[j] += grid[i][j]
		}
	}
	var res int64
	for i := 0; i < n; i++ {
		row := 0
		for j := 0; j < m; j++ {
			row += grid[i][j]
		}
		for j := 0; j < m; j++ {
			if grid[i][j] == 1 {
				res += int64(row-1) * int64(col[j]-1)
			}
		}
	}
	return res
}
